package New.P3;

import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class NetworkInfo {
    public static void main(String[] args) {
        try {
            // 获取本地主机对象
            InetAddress localHost = InetAddress.getLocalHost();
            
            // 获取主机名
            String hostName = localHost.getHostName();
            System.out.println("主机名: " + hostName);
            
            // 获取IP地址
            String hostAddress = localHost.getHostAddress();
            System.out.println("IP地址: " + hostAddress);
        } catch (UnknownHostException e) {
            System.err.println("无法获取本机IP地址及主机名: " + e.getMessage());
            e.printStackTrace();
        }
    }
}